package com.example.demo.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.order.entity.OOrderManage;
import com.example.demo.order.service.OOrderManageService;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/oOrderManage")
@Tag(name = "oOrderManage", description = "订单管理表")
public class OOrderManageController {
    
    @Autowired
    private OOrderManageService oOrderManageService;

    /**
     * 获取订单管理列表
     */
    @GetMapping("/getOrderList")
    @Operation(summary = "获取订单管理列表", description = "获取订单管理列表")
    public Result<?> getOrderList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                 @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                 OOrderManage order){
        Page<OOrderManage> page = new Page<>(current,size);
        QueryWrapper<OOrderManage> queryWrapper = new QueryWrapper<>(order);
        return Result.ok(oOrderManageService.page(page,queryWrapper));
    }

    /**
     * 根据ID查询订单管理信息
     */
    @GetMapping("/getOrderById")
    @Operation(summary = "根据ID查询订单管理信息", description = "根据ID查询订单管理信息")
    public Result<?> getOrderById(Integer id){
        return Result.ok(oOrderManageService.getById(id));
    }

    /**
     * 修改订单管理信息
     */
    @PutMapping("/updateOrder")
    @Operation(summary = "根据id修改订单管理信息", description = "根据id修改订单管理信息")
    @Transactional
    public Result<?> updateOrder(@RequestBody OOrderManage order){
        return Result.ok(oOrderManageService.updateById(order));
    }

    /**
     * 新增订单管理信息
     */
    @PostMapping("/addOrder")
    @Operation(summary = "新增订单管理信息", description = "新增订单管理信息")
    public Result<?> addOrder(@RequestBody OOrderManage order){
        return Result.ok(oOrderManageService.save(order));
    }

    /**
     * 删除订单管理
     */
    @DeleteMapping("/deleteOrderById")
    @Operation(summary = "删除订单管理", description = "删除订单管理")
    @Transactional
    public Result<?> deleteOrderById(Integer id){
        return Result.ok(oOrderManageService.removeById(id));
    }

    /**
     * 获取租户端首页订单状态统计数量
     */
    @GetMapping("/getOrderStatusCount")
    @Operation(summary = "获取租户端首页订单状态统计数量", description = "获取租户端首页订单状态统计数量")
    public Result<?> getOrderStatusCount(String id){
        QueryWrapper<OOrderManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("sum(order_read_flag='0') as orderReadFlagCount," +
                "sum(left(ORDER_DATE,10)=curdate()) as todayOrderCount," +
                "sum(ORDER_STATUS='1') as orderWillCount");
        queryWrapper.eq("SERVICE_STAFF",id);
        OOrderManage oOrderManage = oOrderManageService.getOne(queryWrapper);
        return Result.ok(oOrderManage);
    }

    /**
     * 获取租户端首页用户业绩统计
     */
    @GetMapping("/getUserResult")
    @Operation(summary = "获取租户端首页用户业绩统计", description = "获取租户端首页用户业绩统计")
    public Result<?> getUserResult(String id){
        return oOrderManageService.getUserResult(id);
    }
}

